//
//  ReadModel.m
//  Lucky28
//
//  Created by zr on 2018/8/3.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "ReadModel.h"

@implementation ReadModel

+(instancetype)modelWithDictionary:(NSDictionary *)dic{
    
    ReadModel *model =[[ReadModel alloc] init];
    
    [model setValuesForKeysWithDictionary:dic];
    
    return model;
}


+(void)readWithType:(NSString *)type WithSucess:(void (^)(NSArray<ReadModel *> *))sucessBlock error:(void (^)(void))errorBlock{
    
    NSDictionary * parameters = @{@"type":type,@"is_top":@"0"};
    
    [[NetworkTool sharedTools] POST:@"/index/index/getArticle" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        
        NSArray * paramArray = responseObject[@"data"];
        
        //        NSLog(@"==%@",paramArray);
        
        NSMutableArray *mArray = [NSMutableArray array];
        
        [paramArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            ReadModel * model = [ReadModel modelWithDictionary:obj];
            
            [mArray addObject:model];
            
        }];
        
        if(sucessBlock){
            sucessBlock(mArray.copy);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
}


//搜索
+(void)searchWithKeywords:(NSString *)keywords WithSucess:(void(^)(NSArray<ReadModel *> *array))sucessBlock error:(void(^)(void))errorBlock{
    
    NSDictionary * parameters = @{@"keywords":keywords};
    
    [[NetworkTool sharedTools] POST:@"/index/index/lucksearch" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        
        NSArray * paramArray = responseObject[@"data"];
        
        //        NSLog(@"==%@",paramArray);
        
        NSMutableArray *mArray = [NSMutableArray array];
        
        [paramArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            ReadModel * model = [ReadModel modelWithDictionary:obj];
            
            [mArray addObject:model];
            
        }];
        
        if(sucessBlock){
            sucessBlock(mArray.copy);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
    
}

-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
}


@end
